<template>
  <div class="app-container">
    <el-button type="primary" @click="handleAddUser">新建用户</el-button>

    <el-table :data="userList" style="width: 100%;margin-top:30px;">
      <el-table-column align="center" label="管理员账号" width="220">
        <template slot-scope="scope">
          {{ scope.row.username }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="管理员姓名" width="220">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column align="header-center" label="备注">
        <template slot-scope="scope">
          {{ scope.row.remark }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDelete(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :visible.sync="dialogVisible" :title="dialogType==='edit'?'编辑用户':'新建用户'">
      <el-form :model="user" label-width="80px" label-position="left">
        <el-form-item label="用户名">
          <el-input v-model="user.username" auto-complete="new-password" placeholder="用户名" />
        </el-form-item>
        <el-form-item label="拥有角色">
          <el-select v-model="user.roleIds" class="selector" multiple placeholder="请选择">
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="user.password" auto-complete="new-password" type="password" :placeholder="password_hint" />
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="user.name" placeholder="姓名" />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="user.mobile" placeholder="手机号" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input
            v-model="user.remark"
            :autosize="{ minRows: 2, maxRows: 4}"
            type="textarea"
            placeholder="用户备注"
          />
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { deepClone } from '@/utils'
import { getUsers, addUser, updateUser, deleteUser } from '@/api/user'
import success from '@/utils/operation-message'
import { getRoles } from '@/api/role'

const defaultUser = {
  username: '',
  name: '',
  mobile: '',
  password: '',
  remark: ''
}

export default {
  data() {
    return {
      user: Object.assign({}, defaultUser),
      userList: [],
      roleList: [],
      dialogVisible: false,
      dialogType: 'new',
      defaultProps: {
        children: 'children',
        label: 'title'
      },
      password_hint: '密码'
    }
  },
  created() {
    this.getUsers()
    this.getRoles()
  },
  methods: {
    async getUsers() {
      const res = await getUsers()
      this.userList = res.data
    },
    async getRoles() {
      const res = await getRoles()
      this.roleList = res.data
    },
    handleAddUser() {
      this.user = Object.assign({}, defaultUser)
      if (this.$refs.tree) {
        this.$refs.tree.setCheckedNodes([])
      }
      this.dialogType = 'new'
      this.dialogVisible = true
    },
    handleEdit(scope) {
      this.dialogType = 'edit'
      this.dialogVisible = true
      this.password_hint = '不填写即代表不修改'
      this.user = deepClone(scope.row)
    },
    handleDelete(scope) {
      this.$confirm('您确定要删除该用户吗?', '', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await deleteUser(scope.row.id)
          this.getUsers()
          this.$message({
            type: 'success',
            message: '删除成功'
          })
        })
        .catch(err => { console.error(err) })
    },
    async handleConfirm() {
      const isEdit = this.dialogType === 'edit'
      if (isEdit) {
        await updateUser(this.user, this.user.id)
      } else {
        await addUser(this.user)
      }
      this.getUsers()

      this.dialogVisible = false
      success()
    }
  }
}
</script>
